[WPF] How to register to/listen to richtextbox commando's?

Posted by Liewe on Stack Overflow See other posts from Stack Overflow or by Liewe
Published on 2010-03-08T08:58:05Z Indexed on 2010/03/08 9:06 UTC
Read the original article Hit count: 699

Filed under:
|
|

I'm creating a simple editor within our application using the WPF RichTextBox. Above it I've added the reguslar buttons like Bold, Italic, etc. These buttons use the RichTextBox's commands to set these properties, but next to these buttons, the commands also get send with CTRL+B, CTRL+I, etc. I want these buttons to represent the current state of the RichTextBox at the cursor. I already found out how to get this state and it works when I update this state on the SelectionChanged event. This event ofcourse isn't fired when Bold is toggled so there is no direct feedback.

I would like to know if there is a way to listen to the commands being called, without affecting its original behaviour or some other ideas to solve my problems.

I tried listening to the command the following way:

   CommandBinding boldBinding = new CommandBinding(EditingCommands.ToggleBold, CommandExecuted);
  _richTextBox.CommandBindings.Add(boldBinding);   

and

private void CommandExecuted(object sender, ExecutedRoutedEventArgs e) {
  UpdateProperties();
  e.Handled = false;      
}

This did update the properties, but the RichTextBox didn't seem to receive the command anymore.

I also tried to make my own commands on the control containing the RichTextBox, but when CTRL+B is pressed when the RichTextBox has focus, the original RichTextBox commands are called instead of the new one.

Many thanks in advance!

Liewe

© Stack Overflow or respective owner

Related posts about wpf

Related posts about richtextbox